﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SortGUI//希尔排序
{
    public class ShellSort : SortBase
    {
        override public void Sort(int[] alNum)
        {
            Initialize();
            int n = alNum.Length;
            int gap = n / 2;
            while (gap > 0)
            {
                for (int i = gap; i < n; i++)
                {
                    int key = alNum[i];
                    int j = i;
                    for (j = i; j >= gap && alNum[j - gap] > key; j -= gap)
                    {
                        lastSwappedIndices[0] = j - gap;
                        lastSwappedIndices[1] = j;
                        Swap(ref alNum[j], ref alNum[j - gap]);
                        count++;
                        SortNextStep();
                    }

                    alNum[j] = key;
                }

                gap /= 2;
            }

            Complete();
        }
        public override void Initialize()
        {
            count = 0;
            updataColor(isInitialize = false);
        }

        public override void SortNextStep()
        {
            updateUiCallback(count, lastSwappedIndices, lastSortedIndex = 10000, swapped);
        }

        public override void Complete()
        {
            updateUiCallback(count, lastSwappedIndices, lastSortedIndex - 10000, swapped);
        }
    }
}
